
/*Include and Define section*/
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter.h>
#include <linux/socket.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/moduleparam.h>


/*Global values*/
static int myintArray[6] = { 0, 0, 0, 0, 0, 0 };
static int arr_argc = 0;

/*Command line params*/
module_param_array(myintArray, int, &arr_argc, 0000);
MODULE_PARM_DESC(myintArray, "An array of integers");


/*Netfilter section*/


static struct nf_hook_ops nfho;

/* Hook funktion */
unsigned int hook_func(unsigned int hooknum,
    struct sk_buff **skb,
    const struct net_device *in,
    const struct net_device *out,
    int (*okfn)(struct sk_buff *))
{
 	bool answ=true;
 	struct sk_buff *sb = *skb;
 	int i=0;
 	int r;
  	for(i=0;i<6;i++){
		r=sb->mac.raw[i+6];
		if(r!=myintArray[i])
		answ=false;
		printk("%x ",r);	
	}

	if (!answ)
 	{
 	 	printk(" \n");
 		answ=true;
  		return NF_ACCEPT;
 	}
 	else{
		printk(" Packet dropped...\n");
       		answ=true;
		return NF_DROP;
	}
}



/* /Proc filesystem section*/
static int
mac_filter_proc(char *buffer, char **start, off_t offset, int size, int *eof,
		void *data)
{
	char *hello_str = "Hello, world!\n";
	int len = strlen(hello_str); 
	if (size < len)
		return -EINVAL;
	if (offset != 0)
	strcpy(buffer, hello_str);
	*eof = 1;
	return len;

}

/*Kernel module section*/
static int macfilter_init(void) {
  	int i=0;
  	nfho.hook     = hook_func;
	nfho.hooknum  = NF_IP_PRE_ROUTING;
	nfho.pf       = PF_INET;
	nfho.priority = NF_IP_PRI_FIRST;
 
	nf_register_hook(&nfho);

	if (create_proc_read_entry("mac_filter", 0, NULL, mac_filter_proc,NULL) == 0) {
		printk(KERN_ERR
		       "Unable to register mac_filter proc file\n");
		return -ENOMEM;
	}

	for (i = 0; i < (sizeof myintArray / sizeof (int)); i++)
	{
		printk(KERN_INFO "myintArray[%d] = %x\n", i, myintArray[i]);
	}
	printk(KERN_INFO "got %d arguments for myintArray.\n", arr_argc);


 	 printk(KERN_ALERT "Hello, world\n");
 	 return 0;
}


static void macfilter_exit(void) {
	nf_unregister_hook(&nfho);
	remove_proc_entry("mac_filter", NULL);
 	printk(KERN_ALERT "Goodbye, cruel world\n");
}


module_init(macfilter_init);
module_exit(macfilter_exit);


/*Module info*/
MODULE_LICENSE("GPLv2");
MODULE_AUTHOR("Rodionov Dmitry <RodionovDV@gmail.com>");
MODULE_DESCRIPTION("Netfilter module (filtration by mac)");
MODULE_VERSION("0.9");
